//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus;

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Model.Domain.Reservation;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.bc004346.travelus.Utility.log;

/**
 * Created by Renats on 1/18/14.
 * Handles flight display on the screen
 */
public class FlightFragment extends ListFragment {

    public interface OnFlightSelectionChangeListener {
        public void onFlightSelectionChanged(Flight flight);
    }

    private FullscreenActivity mActivity;
    private OnFlightSelectionChangeListener mListener;

    //<editor-fold desc="Properties">

    public FullscreenActivity getFullScreenActivity() {
        if (mActivity == null)
            mActivity = (FullscreenActivity) getActivity();
        return mActivity;
    }

    public OnFlightSelectionChangeListener getListener() {
        if (mListener == null) {
            mListener = (OnFlightSelectionChangeListener) getActivity();
        }
        return mListener;
    }

    //</editor-fold>

    //<editor-fold desc="Overrides">

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        Object item = l.getItemAtPosition(position);
        if (item != null && item.getClass().equals(Flight.class)) {
            Flight flight = (Flight) l.getItemAtPosition(position);
            if (getListener() != null && flight != null)
                getListener().onFlightSelectionChanged(flight);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        try {
            List<Reservation> reservations = getFullScreenActivity().getSqlHelper().SelectReservations();

            populateListItems(reservations);
        } catch (Exception e) {
            log(e, getFullScreenActivity());
        }
    }

    //</editor-fold>

    public List<Reservation> RefreshFlightList(List<Reservation> reservations) {

        try {
            log(String.format(mActivity.getString(R.string.flightLog_Starting), reservations.size()));

            getFullScreenActivity().getSqlHelper().MergeReservations(reservations);
            reservations = getFullScreenActivity().getSqlHelper().SelectReservations();
            populateListItems(reservations);
            log(String.format(mActivity.getString(R.string.flightLog_End), reservations.size()));
        } catch (Exception e) {
            log(e, getFullScreenActivity());
        }
        return reservations;
    }

    private void populateListItems(List<Reservation> reservations) {

        try {
            final ListView lv = getListView();
            if (reservations.size() > 0) {
                List<Flight> flights = new ArrayList<Flight>();
                for (Reservation res : reservations)
                    for (Flight flight : res.getFlights())
                        flights.add(flight);

                Collections.sort(flights);

                ArrayAdapter<Flight> flightArrayAdapter = new ArrayAdapter<Flight>(getFullScreenActivity(),
                        R.layout.list_layout_flights, flights);
                setListAdapter(flightArrayAdapter);

                if (lv != null) {
                    lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    lv.setSelector(android.R.color.darker_gray);
                }

            } else {
                ArrayAdapter<String> flightArrayAdapter = new ArrayAdapter<String>(getFullScreenActivity(),
                        R.layout.list_layout_flights, new ArrayList<String>() {{
                    add(mActivity.getString(R.string.dashboardStatus_NoFlightsFound));
                }});
                setListAdapter(flightArrayAdapter);
                if (lv != null)
                    lv.setChoiceMode(ListView.CHOICE_MODE_NONE);

            }
        } catch (Exception e) {
            log(e, getFullScreenActivity());
        }
    }
}
